# cython: language_level=3
#!/usr/bin/python
# -- coding:utf8 --

import json
import re
import app.model_phl.my_tool_package as my_package
import utils
import random
import datetime
import numpy as np
import app.model_phl.rules as model_rules
import app.utils.util_log as util_log


def get_rule_result(rule_sets_result):
    if 1 in list(rule_sets_result.values()):
        return 'reject'
    else:
        return 'pass'


def get_is_rule_featues(freatures, key):
    if 'none' == freatures.get(key, 'none'):
        util_log.get_logger().warning('get_is_rule_featues {} error'.format(key))
        return False
    else:
        return True

def get_is_rule_featues_v1(freatures, key):
    if 'none' == freatures.get(key, 'none') or freatures.get(key, 'none') is None or freatures.get(key, 'none') == '':
        util_log.get_logger().warning('get_is_rule_featues {} error'.format(key))
        return True
    else:
        return False

def get_newusers_free_rule_main(free_features):
    '''
    新用户策略主函数
    1.判断是否是白名单，走白名单策略
    2.判断是否是他盘老客，走他盘老客策略
    3.最后走普通策略
    :param free_features:
    :return:
    '''
    # 新用户区分是否有过还款行为
    in_white_list = free_features.get('in_white_list', 0)
    rule_sets_result_all = {}
    # currentAmount = free_features.get('LastAmount', 0)
    # FraudAssessmentResults = {}
    # approveAmount = 0

    # 前置息
    pfRate = 0.42
    # 利息
    interestRate = 0.01

    # 白名单客群
    if in_white_list == 1:  # 白名单
        # 添加跟放产品的信息
        # Easy Loan 跟放产品 We Peso 10058, Peso Market 10050, Smart Money  10052, Fortune Cash  10053
        # Future Cash 跟放产品 Happy Cash 10071, Perfect Cash 10073, Urgent loan 10074, Urgent cash 10075
        # LuckyCash 跟放产品 ExpressPera  10044, KwartaLoan 10045, CashPao 10046, MasayaPera 10054
        # 20240607新增
        # Fast Service 跟放产品 Good Deal 10078, Save Value 10079, Speed Cash 10080, Cash King 10081
        # 20240709新增
        # Quick Delivery 跟放产品
        if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053, 10078, 10079, 10080, 10081, 10071, 10073, 10074, 10075, 10044, 10045, 10046, 10054, 10087, 10088, 10089, 10090]):
            gentou_rules = model_rules.OldCustomerRules()
            # white_rules.initial_abtest(free_features=free_features)
            rule_sets_hit_result = gentou_rules.gentou_rules(free_features=free_features)
            rule_sets_result_all.update(rule_sets_hit_result)
            FraudAssessmentResults = get_rule_result(rule_sets_result_all)
            approveAmount = gentou_rules.gentou_pre_amount(free_features=free_features,
                                                    FraudAssessmentResults=FraudAssessmentResults)
        else:
            white_rules = model_rules.WhiteRules()
            # white_rules.initial_abtest(free_features=free_features)
            rule_sets_hit_result = white_rules.rules(free_features=free_features)
            rule_sets_result_all.update(rule_sets_hit_result)
            FraudAssessmentResults = get_rule_result(rule_sets_result_all)
            approveAmount = white_rules.preloan_amount(free_features=free_features,
                                                    FraudAssessmentResults=FraudAssessmentResults)
            
            # 针对主产品首贷增加人审状态
            # 20240906主产品增加FS和QD全机审
            # FS 10077; QD 10086
            if FraudAssessmentResults == 'pass':
                if int(free_features.get('ProjectId', 0)) in ([10041, 10077, 10086]):
                    if get_is_rule_featues_v1(free_features, 'FaceCompareScore'):
                        FraudAssessmentResults = 'manual'
                    else:
                        if float(free_features.get('FaceCompareScore', 0)) <= 70:
                            FraudAssessmentResults = 'manual'
    
                # 20240914增加人审逻辑
                if get_is_rule_featues_v1(free_features, 'XWWhitListScoreV2'):
                    FraudAssessmentResults = 'manual'
                else:
                    if float(free_features.get('XWWhitListScoreV2', 0)) <= 0:
                        FraudAssessmentResults = 'manual'
                # 增加人审逻辑
                if get_is_rule_featues_v1(free_features, 'XWWhitListScoreV2') | float(free_features.get('XWWhitListScoreV2', 0)) <= 0:
                    FraudAssessmentResults = 'manual'

    else:
        not_white_rules = model_rules.NewCustomerRules()
        rule_sets_hit_result = not_white_rules.rules(free_features=free_features)
        rule_sets_result_all.update(rule_sets_hit_result)
        FraudAssessmentResults = get_rule_result(rule_sets_result_all)
        approveAmount = not_white_rules.preloan_amount(free_features=free_features,
                                                   FraudAssessmentResults=FraudAssessmentResults)

    # 跟放产品首贷，使用老客评分 oldCreditScoreV1
    if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053, 10078, 10079, 10080, 10081, 10071, 10073, 10074, 10075, 10044, 10045, 10046, 10054, 10087, 10088, 10089, 10090]):
        newCreditScore = free_features.get('XWOldScoreV1', 0)
    # 主产品LuckyCash和FutureCash使用自研评分V4版本 CreditScoreV4
    elif int(free_features.get('ProjectId', 0)) in ([10070]):
        newCreditScore = free_features.get('CreditScoreV4', 0)
    # 20240904主产品使用阿伟新客评分V2版本
    else:
        newCreditScore = int(float(free_features.get('XWWhitListScoreV2', 0)))

    return {'input_params': free_features,
            'rule_sets_result': rule_sets_result_all,
            'FraudAssessmentResults': FraudAssessmentResults,
            'CreditScore': newCreditScore,
            'approveAmount': approveAmount,
            'pfRate': pfRate,
            'interestRate': interestRate,
            }


def get_oldusers_free_rule(free_features):
    # 前置息和日利息
    pfRate = 0.42
    interestRate = 0.01
    currentAmount = free_features.get('current_amount', 0)

    rule_sets_result_all = {}
    
    # 添加跟放产品的信息
    # Easy Loan 跟放产品 We Peso 10058, Peso Market 10050, Smart Money  10052, Fortune Cash  10053
    # Future Cash 跟放产品 Happy Cash 10071, Perfect Cash 10073, Urgent loan 10074, Urgent cash 10075
    # LuckyCash 跟放产品 ExpressPera  10044, KwartaLoan 10045, CashPao 10046, MasayaPera 10054
    # 20240607新增
    # Fast Service 跟放产品 Good Deal 10078, Save Value 10079, Speed Cash 10080, Cash King 10081

    if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053, 10078, 10079, 10080, 10081, 10071, 10073, 10074, 10075, 10044, 10045, 10046, 10054, 10087, 10088, 10089, 10090]):
            gentou_rules = model_rules.OldCustomerRules()
            # white_rules.initial_abtest(free_features=free_features)
            # 20240912 跟投复贷策略
            rule_sets_hit_result = gentou_rules.gentou_rules_old(free_features=free_features)
            rule_sets_result_all.update(rule_sets_hit_result)
            FraudAssessmentResults = get_rule_result(rule_sets_result_all)
            # 跟投产品提额策略
            approveAmount, upAmount = gentou_rules.gentou_amount_v1(free_features=free_features,
                                                                    FraudAssessmentResults=FraudAssessmentResults)
    else:
        old_customer_rules = model_rules.OldCustomerRules()
        # ABTest操作
        # old_customer_rules.initial_abtest(free_features=free_features)
        rule_sets_hit_result = old_customer_rules.rules(free_features=free_features)
        rule_sets_result_all.update(rule_sets_hit_result)
        FraudAssessmentResults = get_rule_result(rule_sets_result_all)
        # 提额策略
        approveAmount, upAmount = old_customer_rules.inloan_amount_v1(free_features=free_features,
                                                                    FraudAssessmentResults=FraudAssessmentResults)
    
    # if int(free_features.get('ProjectId', 0)) == 10041:
    #     OldCreditScore = int(float(free_features.get('XWOldScoreV1', 0)))
    # else:
    #     OldCreditScore = free_features.get('oldCreditScoreV1', 0)

    OldCreditScore = int(float(free_features.get('XWOldScoreV1', 0)))
    # 返回结果
    return {'input_params': free_features,
            'rule_sets_result': rule_sets_result_all,
            'FraudAssessmentResults': FraudAssessmentResults,
            'OldCreditScore': OldCreditScore,
            'approveAmount': approveAmount,
            'pfRate': pfRate,
            'interestRate': interestRate,
            'currentAmount': currentAmount,
            'upAmount': upAmount,
            }
